package com.zeedev.flyingsaucer.beers.activities.beerlists;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.name.Named;
import com.zeedev.flyingsaucer.adapter.BeerTreeAdapter;
import com.zeedev.flyingsaucer.beers.activities.beerdialog.BeerDialog2;
import com.zeedev.flyingsaucer.modules.DownloadModule.interfaces.DownloadManager;
import com.zeedev.flyingsaucer.ufoclub.R;
import mobi.zeedev.flyingsaucer.common.DataModule.MetaBeer;
import roboguice.activity.RoboExpandableListActivity;

public class BeerTree extends RoboExpandableListActivity {

    private Cursor mCursor;
    private BeerTreeAdapter mAdapter;

    @Inject
    @Named("groupBy")
    Provider<String> groupBy;

    @Inject
    DownloadManager downloadManager;

    @Inject
    @Named("treeCursor")
    Provider<Cursor> mCursorProvider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mCursor = mCursorProvider.get();

        mAdapter = new BeerTreeAdapter(this, mCursor, groupBy.get(), false);
        this.setListAdapter(mAdapter);

        this.getExpandableListView().setGroupIndicator(null);

        ColorDrawable dw2 = new ColorDrawable(0x00000000);

        this.getExpandableListView().setDivider(dw2);
        this.getExpandableListView().setChildDivider(dw2);

        getExpandableListView().setFastScrollEnabled(true);


//        FrozenExpandableListBehavior.bind(this);


//        autoOpenChildren();
//        expandVisibleGroups(true);

        expandAllGroups();


    }

/*    @Override
    protected void onResume() {
        super.onResume();
//		Util.expandAllGroups(this);
//        expandVisibleGroups(true);
    }*/

/*    @Override
    public void update(Observable observable, Object data) {
        this.mCursor.requery();
//		Util.expandAllGroups(this);
//		this.getExpandableListView().set
    }*/

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Menu
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.date_style, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_lookup_refresh:

                downloadManager.startRefresh();

                return true;
            default:
                return false;
        }
//	      return false;
    }

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Activity Life-cycle Actions
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        Cursor o = this.mAdapter.getChild(groupPosition, childPosition);
        Integer beerId = o.getInt(o.getColumnIndex(MetaBeer.BEER_ID));
        Intent d = BeerDialog2.makeDialog(this, beerId);
        this.startActivityForResult(d, 0);
        return super.onChildClick(parent, v, groupPosition, childPosition, id);
    }

    private void expandAllGroups() {
        for (int i = 0; i < this.getExpandableListView().getCount(); i++) {


            this.getExpandableListView().expandGroup(i);


        }
    }


//	@Override
//	public void onGroupCollapse(int groupPosition) {
//		super.onGroupCollapse(groupPosition);
//		this.getExpandableListView().expandGroup(groupPosition);
//	}
/*

    public void expandVisibleGroups(boolean first) {
//		int start = 0;
        int start = this.getExpandableListView().getFirstVisiblePosition();
//		int end = this.getExpandableListView().getLastVisiblePosition();
//		int end = this.getExpandableListView().getCount();
        int end = this.getExpandableListView().getLastVisiblePosition();

        if (first) {
            for (int i = 0; i < 9; i++) {
                this.getExpandableListView().expandGroup(i);
            }
        } else {
            for (int i = start; i < end; i++) {
                this.getExpandableListView().expandGroup(i);
            }
        }
        Ln.d("expandVisibleGroups:" + start + "," + end);
//		this.getExpandableListView().scrollBy(0, 30);
    }
    }


    public void autoOpenChildren() {
        //open currently viewable items


        this.getExpandableListView().setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                for (int i = firstVisibleItem; i < (visibleItemCount + firstVisibleItem); i++) {
                    getExpandableListView().expandGroup(i);
                }
                Ln.d("onScroll:" + firstVisibleItem + "," + visibleItemCount + "," + totalItemCount);

            }

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                //do nothing
                Ln.d("onScrollStateChanged");

            }
        });
    */

}
